﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using LocalSettings;
using QuickTasks.ViewModels.Tables;
using System.Collections.ObjectModel;

namespace QuickTasks
{
    public class QuickLinks
    {
       

        #region events

        public void SaveLinks(string name, string link1, string link2, string link3, string link4, string link5, string link6,string linkid)
        {
            if (linkid != null)
            {
                try
                {
                    using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                    {
                        string links = "";

                        links = link1;


                        if (link2 != "") links = links + "," + link2;
                        if (link3 != "") links = links + "," + link3;
                        if (link4 != "") links = links + "," + link4;
                        if (link5 != "") links = links + "," + link5;
                        if (link6 != "") links = links + "," + link6;
                        IQueryable<Links> linkid1 = from a in context.link where a.id.ToString() == linkid select a;

                        Links link = linkid1.FirstOrDefault();
                        {


                            link.name = name;
                            link.linkelement = links;
                            

                        };

                        context.SubmitChanges();
                    }
                }


                catch (Exception ex)
                {
                    Exceptions.SaveOrSendExceptions("Exception in SaveLinks method In QuickLinks file", ex);
                }
            }
            else
            {
                int tid = 0;
                try
                {
                    string DefaultPlace = "";
                    if (IsolatedSettings.getSettingsStringValue("DefaultPlace") != "")
                    {
                        DefaultPlace = IsolatedSettings.getSettingsStringValue("DefaultPlace");
                    }
                    else
                    {
                        DefaultPlace = "1";
                    }
                    string links = "";

                    links = link1;


                    if (link2 != "") links = links + "," + link2;
                    if (link3 != "") links = links + "," + link3;
                    if (link4 != "") links = links + "," + link4;
                    if (link5 != "") links = links + "," + link5;
                    if (link6 != "") links = links + "," + link6;

                    using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                    {
                        var xquery = from a in context.link
                                     orderby a.id descending
                                     select a.id;
                        if (xquery.Count() <= 0)
                        {
                            tid = 1;
                        }
                        else
                        {
                            var xquery1 = from a in context.link
                                          orderby a.id descending
                                          select a;
                            foreach (var query in xquery1)
                            {
                                tid = Convert.ToInt32(query.id) + 1;
                                break;
                            }
                        }
                        Links link = new Links()
                        {
                            id = tid,
                            name = name,
                            place = DefaultPlace,
                            linkelement = links
                        };
                        context.link.InsertOnSubmit(link);
                        context.SubmitChanges();
                    }
                }
                catch (Exception ex)
                {
                    Exceptions.SaveOrSendExceptions("Exception in SaveLinks method In QuickLinks file", ex);
                }
            }
        }

        public List<QuickLinkProp> LoadLinksDetails()
        {
            List<QuickLinkProp> objQuicklinkList = new List<QuickLinkProp>();
            try
            {
                using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                {
                    var eleQuickLink = from a in context.link select a;
                    foreach (var eleLink in eleQuickLink)
                    {
                    
                       
                            QuickLinkProp objQuickLink = new QuickLinkProp();
                            objQuickLink.Id = eleLink.id.ToString();
                            objQuickLink.Name = eleLink.name;
                            objQuicklinkList.Add(objQuickLink);
                       
                       
                        
                    }

                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in LoadLinksDetails method In QuickLinks file", ex);
            }
            return objQuicklinkList;
        }

        public List<string> GetLinks(string id)
        {
            List<string> links = new List<string>();
            try
            {
                using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                {
                    var eleLinks = from m in context.link where m.id.ToString() == id select m;
                    foreach (var eleLink in eleLinks)
                    {

                        string[] link = eleLink.linkelement.ToString().Split(new char[] { ',', ';' });
                        foreach (string str1 in link)
                        {

                            links.Add(str1);
                        }
                    }
                }
                
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in GetLinks method In QuickLinks file", ex);
            }
            return links;
        }

        public ObservableCollection<QuickLinkProp> LoadLinksDetailsSameData(string id)
        {
            ObservableCollection<QuickLinkProp> objQuicklinkList = new ObservableCollection<QuickLinkProp>();
            try
            {
                using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                {
                    var eleQuickLink = from i in context.link where i.place == id select i;
                    foreach (var eleLink in eleQuickLink)
                    {
                        QuickLinkProp objQuickLink = new QuickLinkProp();
                        objQuickLink.Id = eleLink.id.ToString();
                        objQuickLink.Name = eleLink.name;
                        objQuicklinkList.Add(objQuickLink);
                    }
                }
               
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in LoadLinksDetailsSameData method In QuickLinks file", ex);
            }
            return objQuicklinkList;
        }

        #endregion
    }
}
